﻿using System;
using System.Linq;
using LX.Core.Validation;
using S4Store.Domain.CarKpi;
using S4Store.Persistent;
using S4Store.Domain.SellerKpi;
using S4Store.Domain.FinancialKpi;

namespace S4Store.Service.Valid.FinancialKpiValid
{
    public class FinancialMonthConfigSave : ValidExpression<FinancialMonthConfig>
    {
        public S4StoreDataContext PersistentContext { get; set; }

        public override void ValidEntity(ValidationResult result, FinancialMonthConfig entity)
        {
            if (entity.Month == DateTime.MinValue)
            {
                PushError(result, "日期不正确");
            }
            if (entity.StoreId == 0)
            {
                PushError(result, "店不正确");
            }

            if (PersistentContext.Set<FinancialMonthConfig>().AsQueryable().Any(t => t.StoreId == entity.StoreId && t.Month.Year == entity.Month.Year && t.Month.Month == entity.Month.Month && t.Id != entity.Id))
            {
                PushError(result, "这个月已经有一条指标记录了");
            }
        }

    }
}
